package com.jboltai.capability.functioncall;

import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.lang.reflect.Type;


/**
 * 函数参数定义
 */
public class FunctionParam implements Serializable {

    /**
     * 参数名
     */
    private String name;
    /**
     * 参数类型
     */
    private String type;

    /**
     * 该参数是否必填
     */
    private Boolean required;

    /**
     * 参数描述
     */
    private String description;
    /**
     * 参数默认值
     */
    private Object defaultValue;

    public FunctionParam(String name, Type type, Boolean required, Object defaultValue, String description) {
        this.name = name;
        this.type = type.getTypeName();
        this.required = required;
        this.description = description;
        this.defaultValue = defaultValue;
    }
    public FunctionParam(String name, String type, Boolean required, Object defaultValue, String description) {
        this.name = name;
        this.type = type;
        this.required = required;
        this.description = description;
        this.defaultValue = defaultValue;
    }

    public Object getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(Object defaultValue) {
        this.defaultValue = defaultValue;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Boolean getRequired() {
        return required;
    }

    public void setRequired(Boolean required) {
        this.required = required;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public JSONObject toJSONObject() {
        JSONObject o = new JSONObject();
        o.put("name", this.name);
        o.put("required", this.required);
        o.put("description", this.description);
        o.put("defaultValue", this.defaultValue);
        String type = this.type.toLowerCase();
        o.put("type", "string");
        //如果type是java中的数字类型，则转换为number
        if(type.endsWith("integer") || type.endsWith("long") || type.endsWith("double") || type.endsWith("float") || type.endsWith("bigdecimal")){
            o.put("type", "number");
        }
        //如果type是java中的boolean类型，则转换为boolean
        else if(type.endsWith("boolean")){
            o.put("type", "boolean");
        }
        //如果type是java中的集合类型 数组类型 就转为array
        else if(type.endsWith("list") || type.endsWith("set") || type.endsWith("array")){
            o.put("type", "array");
        }
        //如果type是java中的对象类型，则转换为object
        else if(!type.contains("java.")&&!type.contains("sun.")){
            o.put("type", "object");
        }
        return o;
    }

}
